{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from nltk.corpus import wordnet as wn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('stain.n.01'),\n",
       " Synset('stain.n.02'),\n",
       " Synset('dirt.n.02'),\n",
       " Synset('mark.n.06'),\n",
       " Synset('blot.n.02'),\n",
       " Synset('stain.v.01'),\n",
       " Synset('stain.v.02'),\n",
       " Synset('tarnish.v.01'),\n",
       " Synset('stain.v.04')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('stain')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ss2 = wn.synsets('stain')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Synset.definition of Synset('stain.n.02')>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ss2.definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('microscopy.n.01')]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ss2.topic_domains()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lem = ss2.lemmas()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "u'stain'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lem.name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w1 = 'stain'\n",
    "w2 = 'blood'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[u'fall']],\n",
       " [[], [], [], []],\n",
       " [[u'descent'], [], [], [], [], []],\n",
       " [[u'fall'], [], [], []],\n",
       " [[], [], [], [], [], []],\n",
       " [[], [], []],\n",
       " [[], []],\n",
       " [[], [], []],\n",
       " [[], [], [], []],\n",
       " [[], []],\n",
       " [[u'fall'], [], [], [], [], [], []],\n",
       " [[], [], []],\n",
       " [[u'lie_down', u'sit_down'], [], [], [], []],\n",
       " [[], [], []],\n",
       " [[], [], [], []],\n",
       " [[], [], [], [], [], [], []],\n",
       " [[], [], []],\n",
       " [[u'wane'], [], [], []],\n",
       " [[], []],\n",
       " [[u'go_to_bed'], [u'turn_in'], [], [], []],\n",
       " [[], [], []],\n",
       " [[]],\n",
       " [[]],\n",
       " [[], [], [], []],\n",
       " [[], []],\n",
       " [[u'set'], [], [], []],\n",
       " [[], [], []]]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[[ant.name() for ant in lemma.antonyms()] for lemma in ss.lemmas()] for ss in wn.synsets('rise')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets(w1)[0].lemmas()[0].antonyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.09090909090909091"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.path_similarity(wn.synsets(w1)[0],wn.synsets(w2)[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'stain%1:27:00::'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ss2.lemmas[0].key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lemma('stain.n.02.stain')\n"
     ]
    }
   ],
   "source": [
    "lemma_stain = wn.lemma_from_key('stain%1:27:00::')\n",
    "print lemma_stain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Lemma('stain.v.04.stain')]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lemma_stain.derivationally_related_forms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lemma('cell.n.02.cell')\n"
     ]
    }
   ],
   "source": [
    "lemma_cell = wn.lemma_from_key('cell%1:03:00::')\n",
    "print lemma_cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lemma_cell.entailments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Lemma('stain.n.01.stain'),\n",
       " Lemma('stain.n.01.discoloration'),\n",
       " Lemma('stain.n.01.discolouration')]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('stain')[0].lemmas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('cell.n.01'),\n",
       " Synset('cell.n.02'),\n",
       " Synset('cell.n.03'),\n",
       " Synset('cell.n.04'),\n",
       " Synset('cellular_telephone.n.01'),\n",
       " Synset('cell.n.06'),\n",
       " Synset('cell.n.07')]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(biology) the basic structural and functional unit of all organisms; they may exist as independent units of life (as in monads) or may form colonies or tissues as in higher plants and animals'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Lemma('cellular_telephone.n.01.cellular_telephone'),\n",
       " Lemma('cellular_telephone.n.01.cellular_phone'),\n",
       " Lemma('cellular_telephone.n.01.cellphone'),\n",
       " Lemma('cellular_telephone.n.01.cell'),\n",
       " Lemma('cellular_telephone.n.01.mobile_phone')]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[4].lemmas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('living_thing.n.01')]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hypernyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('whole.n.02')]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hypernyms()[0].hypernyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('object.n.01')]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hypernyms()[0].hypernyms()[0].hypernyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('physical_entity.n.01')]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('entity.n.01')]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Synset('zygote.n.01'),\n",
       " Synset('akaryocyte.n.01'),\n",
       " Synset(\"kupffer's_cell.n.01\"),\n",
       " Synset('blastema.n.01'),\n",
       " Synset('arthrospore.n.02'),\n",
       " Synset('reproductive_cell.n.01'),\n",
       " Synset('plant_cell.n.01'),\n",
       " Synset('blastomere.n.01'),\n",
       " Synset('polar_body.n.01'),\n",
       " Synset('archespore.n.01'),\n",
       " Synset('flagellated_cell.n.01'),\n",
       " Synset('parthenote.n.01'),\n",
       " Synset('fiber.n.03'),\n",
       " Synset('sertoli_cell.n.01'),\n",
       " Synset('embryonic_cell.n.01'),\n",
       " Synset('daughter_cell.n.01'),\n",
       " Synset('arthrospore.n.01'),\n",
       " Synset('mother_cell.n.01'),\n",
       " Synset('gametocyte.n.01'),\n",
       " Synset('recombinant.n.01'),\n",
       " Synset('beta_cell.n.01'),\n",
       " Synset('leydig_cell.n.01'),\n",
       " Synset('somatic_cell.n.01')]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wn.synsets('cell')[1].hyponyms()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sent = 'Americans foresee unending economic doom'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Synset('american.n.01'), Synset('american_english.n.01'), Synset('american.n.03')]\n",
      "Synset('american.n.01') a native or inhabitant of the United States\n",
      "Synset('american_english.n.01') the English language as used in the United States\n",
      "Synset('american.n.03') a native or inhabitant of a North American or Central American or South American country\n",
      "\n",
      "[Synset('anticipate.v.03'), Synset('envision.v.02'), Synset('anticipate.v.02')]\n",
      "Synset('anticipate.v.03') realize beforehand\n",
      "Synset('envision.v.02') picture to oneself; imagine possible\n",
      "Synset('anticipate.v.02') act in advance of; deal with ahead of time\n",
      "\n",
      "[Synset('ageless.s.01')]\n",
      "Synset('ageless.s.01') continuing forever or indefinitely\n",
      "\n",
      "[Synset('economic.a.01'), Synset('economic.a.02'), Synset('economic.s.03'), Synset('economic.s.04'), Synset('economic.s.05')]\n",
      "Synset('economic.a.01') of or relating to an economy, the system of production and management of material wealth\n",
      "Synset('economic.a.02') of or relating to the science of economics\n",
      "Synset('economic.s.03') using the minimum of time or resources necessary for effectiveness\n",
      "Synset('economic.s.04') concerned with worldly necessities of life (especially money)\n",
      "Synset('economic.s.05') financially rewarding\n",
      "\n",
      "[Synset('doom.n.01'), Synset('destine.v.01'), Synset('sentence.v.01'), Synset('doom.v.03')]\n",
      "Synset('doom.n.01') an unpleasant or disastrous destiny\n",
      "Synset('destine.v.01') decree or designate beforehand\n",
      "Synset('sentence.v.01') pronounce a sentence on (somebody) in a court of law\n",
      "Synset('doom.v.03') make certain of the failure or destruction of\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for word in sent.split(' '):\n",
    "    print wn.synsets(word)\n",
    "    for synset in wn.synsets(word):\n",
    "        print synset, synset.definition\n",
    "    print"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
